<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_Doctrine
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * @see SmartL_Profiler_Custom_Abstract
 */
require_once 'SmartL/Profiler/Custom/Abstract.php';

/**
 * Custom profiler for Doctrine.
 *
 * @package SmartL_Doctrine
 */
class SmartL_Doctrine_Profiler extends SmartL_Profiler_Custom_Abstract {
	/**
	 * Doctrine's connection profiler.
	 *
	 * @var Doctrine_Connection_Profiler
	 */
	private $_connectionProfiler;
	
	/**
	 * Inicializes doctrine's connection profiler.
	 *
	 */
	public function __construct() {
		$this->_connectionProfiler = new Doctrine_Connection_Profiler ( );
	}
	
	/**
	 * Returns original Doctrine's connection profiler. 
	 *
	 * @return Doctrine_Connection_Profiler
	 */
	public function getDoctrineConnectionProfiler() {
		return $this->_connectionProfiler;
	}
	
	/**
	 * Add connection to be listened.
	 *
	 * @param Doctrine_Connection $connection
	 */
	public function addConnection(Doctrine_Connection $connection) {
		$connection->addListener ( $this->_connectionProfiler );
	}
	
	/**
	 * @return string
	 */
	public function getName() {
		return 'Doctrine profiler';
	}
	
	/**
	 * @return array
	 */
	public function getHeaders() {
		return array ('name', 'sql', 'time[ms]' );
	}
	
	/**
	 * @return ArrayIterator
	 */
	public function getValuesIterator() {
		$values = array ();
		foreach ( $this->_connectionProfiler->getIterator () as $event ) {
			$values [] = array ($event->getName (), $event->getQuery (), $event->getElapsedSecs () );
		}
		return $values;
	}
}